/**
 * 分页器
 */
class paginator {

	/**
	 * 总页数
	 */
	pageCount = 1

	/**
	 * 当前页码
	 */
	page = 1

	/**
	 * 服务端返回参数key配置
	 */
	configKey = {
		pageCount: 'total_page',
		page: 'page'
	}
	/**
	 * 构造方法
	 */
	constructor($app) {
		this.$app = $app
	}
	/**
	 * 获取数据
	 */
	getData(url, data, fn) {
		let $this = this
		data.page = this.page
		$this.$app.get(url, data).then(data => {
			$this.pageCount = data[$this.configKey.pageCount] ? data[$this.configKey.pageCount] : $this.pageCount
			data.nomore = $this.pageCount == this.page
			fn && typeof fn == 'function' && fn(data)
		});
	}
	/**
	 * 获取数据
	 */
	postData(url, data, fn) {
		// this
		let $this = this
		data.page = this.page
		// 发起请求
		$this.$app.post(url, data, (data => {
			$this.pageCount = data[$this.configKey.pageCount] ? data[$this.configKey.pageCount] : $this.pageCount
			data.nomore = $this.pageCount == this.page
			fn && typeof fn == 'function' && fn(data)
		}));
	}
	/**
	 * 下一页
	 */
	nextPager(fn) {
		this.page += 1
		if (this.page <= this.pageCount && this.pageCount > 1) {
			fn && typeof fn == 'function' && fn()
		} else {
			this.page -= 1
		}
	}

}

export default paginator
